package com.bzg.manager.exception;

import java.util.UUID;

/**
 * Description:自定义异常基类，所有业务异常必须继承此异常
 *
 * @author gmy
 * @date 2017年3月3日 下午2:22:20
 */
public class BaseException extends RuntimeException{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4384303424263379841L;
	
	/**
	 * 异常ID，用于表示某一异常实例，每一个异常实例都有一个唯一的异常ID
	 */
	protected final String id;
	
	/**
	 * 异常信息，包含必要的上下文业务信息，用于打印日志
	 */
	protected final String message;

	/**
	 * 具体异常码，由各具体异常实例化时自己定义
	 */
	protected final String defineCode;

	public String getId() {
		return id;
	}

	public String getMessage() {
		return message;
	}

	public String getDefineCode() {
		return defineCode;
	}
	
	/**
	 * 实例化异常
	 * 
	 * @param msgFormat
	 * @param args
	 * @return
	 */
	public BaseException newInstance(String msgFormat, Object... args) {
		return new BaseException(this.defineCode, msgFormat, args);
	}

	public BaseException(String defineCode, String message) {
		super(message);
		this.id = UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
		this.defineCode = defineCode;
		this.message = message;
	}
	public BaseException(String defineCode, String msgFormat, Object... args) {
		super(String.format(msgFormat, args));
		this.id = UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
		this.defineCode = defineCode;
		this.message = String.format(msgFormat, args);
	}


	public BaseException(String defineCode, String message, Throwable cause) {
		super(message, cause);
		this.id = UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
		this.defineCode = defineCode;
		this.message = message;
	}
	
	public BaseException(String defineCode,String msgFormat, Throwable cause, Object... args) {
		super(String.format(msgFormat, args), cause);
		this.id = UUID.randomUUID().toString().toUpperCase().replaceAll("-", "");
		this.defineCode = defineCode;
		this.message = String.format(msgFormat, args);
	}
}
